function [NumPreviousEvents, NumEvents] = a_9_countEvents_ByTimeAndLocation(tt_vec, kk_vec, Tmax, K)
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	% This function takes as input information about events: their date (turned into an integer) and location.
	% It counts the number of events that happen in each location...:
	%    - strictly before period tt=1 (aggregating across all past periods before tt=1)
	%    - within each period tt from tt=1 to Tmax
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Inputs:
	% tt_vec:				dim1 x 1  (integers that can be negative and above Tmax)
	% kk_vec:				dim1 x 1 (integers between 1 and K)
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Outputs:
	% NumPreviousEvents:	1 x K
	% NumEvents:			T x K
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	%%% Separate indexes of events that happen before and during period of interest (from 1 to Tmax)
	idxes_beforeTimeWindow = find(~isnan(tt_vec) & tt_vec < 1);
	idxes_duringTimeWindow = find(~isnan(tt_vec) & tt_vec >= 1 & tt_vec <= Tmax); % Filter out observations that are outside time window (1 <= tt <= Tmax)	
	kk_vec_before = kk_vec(idxes_beforeTimeWindow);
	kk_vec_during = kk_vec(idxes_duringTimeWindow);
	tt_vec_during = tt_vec(idxes_duringTimeWindow);
	
	% Count all events that happened before period of interest
	NumPreviousEvents = accumarray(kk_vec_before, kk_vec_before, [K 1], @length)'; % 1 x K
	
	% Count all events that happened during period of interest
	NumEvents = accumarray([tt_vec_during kk_vec_during], tt_vec_during, [Tmax K], @length); % Tmax x K
end
